package com.example.springboot.controller;

import com.example.springboot.Service.IAdminService;
import com.example.springboot.common.Result;
import com.example.springboot.controller.dto.LoginDTO;
import com.example.springboot.controller.request.AdminPageRequest;
import com.example.springboot.controller.request.LoginRequest;
import com.example.springboot.controller.request.PasswordRequest;
import com.example.springboot.entity.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    IAdminService adminService;
    @PostMapping("/login")
    public Result login(@RequestBody LoginRequest request){
        LoginDTO login= adminService.login(request);
//        if(login==null){
//            return Result.error("用户名和或密码错误");
//        }
        return Result.success(login);
    }
    //修改密码
    @PutMapping("/password")
    //@RequestBody 把Jason转换成User对象
    public Result password(@RequestBody PasswordRequest request){
        adminService.changePass(request);
        return Result.success();
    }
//四种注解表示，新增查询，删除，修改
    @PostMapping("/save")
    //@RequestBody 把Jason转换成User对象
    public Result save(@RequestBody Admin obj){
        adminService.save(obj);
        return Result.success();
    }
    @PutMapping("/update")
    //@RequestBody 把Jason转换成User对象
    public Result update(@RequestBody Admin obj){
        adminService.update(obj);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        Admin users=adminService.getById(id);
        return Result.success(users);
    }
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
adminService.deleteById(id);
        return Result.success();
    }
    @GetMapping("/list")
//    public List<Admin> listUser(){
//        return adminService.listUsers();
//    }
        public Result list(){
       List<Admin> users=adminService.list();
        return Result.success(users);
    }
    @GetMapping("/page")
    //该位置不能用BaseRequest,因为用了就不能有三个值传入了
    public Result page(AdminPageRequest adminPageRequest){
        adminService.page(adminPageRequest);
        return Result.success(adminService.page(adminPageRequest));
    }
}
